<template>
    <div v-show="modelValue">
        <!--        图片-->
        <div v-if="props.type === 'image'">
            <!--  url-list  用于预览的图片链接列表   hide-on-click-modal 是否可以通过点击遮罩层关闭预览-->
            <el-image-viewer
                v-if="previewLists.length"
                :url-list="previewLists"
                hide-on-click-modal
                @close="handleClose"
            />
        </div>
        
        <!--        视频-->
        <div v-if="props.type === 'video'">
            <el-dialog v-model="visible" width="740px" title="视频预览" :before-close="handleClose">
                <video-player ref="playerRef" :src="url" width="100%" height="450px"/>
            </el-dialog>
        </div>
    </div>
</template>

<script lang="ts" setup>


import {Ref} from "vue";

const props = defineProps({
    modelValue: {
        type: Boolean,
        default: false
    },
    url: {
        type: String,
        default: ''
    },
    type: {
        type: String,
        default: 'image'
    }
})


const playerRef = shallowRef()
const previewLists: Ref<string []> = ref([])


/**
 * 监听 如果显示播放， 如果不显示暂停
 */
watch(
    () => props.modelValue,
    (value) => {
        if (value) {
            nextTick(() => {
                previewLists.value = [props.url]
                playerRef.value?.play()
            })
        } else {
            nextTick(() => {
                previewLists.value = []
                playerRef.value?.pause()
            })
        }
    }
)


const emit = defineEmits(['update:modelValue'])

const handleClose = () => {
    emit('update:modelValue', false)
}


const visible = computed({
    get() {
        return props.modelValue
    },
    
    set(value) {
        emit('update:modelValue', value)
    }
})


</script>
